<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="100px" :rules="rules">
      <el-row style="text-align: center" >
        <el-col :span="12">
          <el-form-item label="当前工位:" prop="uloc">
            <el-input v-model="formModel.uloc" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工艺路线:" prop="tmBasRouteId">
            <el-select v-model="formModel.tmBasRouteId" :disabled="disabledRoute" clearable filterable>
              <el-option v-for="item in routeOptions" :key="item.tmBasRouteId" :label="item.routeName" :value="item.tmBasRouteId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="变速箱编号:" prop="scanTrackNo">
            <el-input ref="scanTrackNo" v-model="formModel.scanTrackNo" @blur="inputblur" @keyup.enter.native="handlerScan"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="submit-area" style="text-align:left;margin-bottom:16px;margin-top:0px;">
        <el-col :span="24">
          <el-button v-show="showSelect" type="primary" v-if="hasPermission('gearbox_launch_select')" plain @click="queryList(false)">查询</el-button>
        </el-col>
      </el-row>
    </el-form>
    <hey-table ref="table" :table-data="tableData" :hasPagination="false" :columns="columns">

    </hey-table>
  </div>
</template>
<script>

  export default {
    name: 'GearboxLaunchList',
    components: {

    },
    props: {},
    data() {
      return {
        timer:null,
        timeOut:null,
        disabledUloc:true,
        disabledRoute:true,
        showSelect:true,
        routeOptions:[],
        tableData:[],
        freshenTime:'',
        formModel: {
          tmBasRouteId:'',
          tmBasUlocId:'',
          ulocNo:'',
          uloc:'',
          scanTrackNo:'',
        },
        rules: {

        },
        columns: [
          { title: '车辆VIN号', key: 'vin'},
          { title: '工艺路线代码', key: 'routeNo'},
          { title: '工艺路线名称', key: 'routeName'},
          { title: '配置描述', key: 'materialDesc'},
          { title: '发动机编号', key: 'engineNo'},
          { title: '变速箱编号', key: 'gearboxNo'},
          { title: '装配工位代码', key: ''},
          { title: '装配工位名称', key: ''},
          { title: '是否已扫描', key: 'isScaned', clcd: 'ind_cd' },
          { title: '扫描时间', key: 'createTime'},
          { title: '扫描用户', key: 'createUsername'},
        ],
      }
    },
    computed: {

    },
    mounted() {
      this.$form = this.$refs.form
      this.initRules()
      //校验用户工位
      this.checkUserULoc()
      this.getFreshenTime()
    },
    beforeDestroy() {
      if(this.timer){
        clearInterval(this.timer)
      }
      if(this.timeOut){
        clearTimeout(this.timeOut)
      }
    },
    methods: {
      handlerScan(){
        if(this.tableData.length==0){
          this.$warn('请先查询打印列表!')
          return false
        }
        if(!this.formModel.scanTrackNo){
          this.$warn('变速箱编号不能为空!')
          return false
        }
        const obj = this.tableData.find(item => item.isScaned === '0')
        if(!obj.engineNo){
          this.$warn('当前VIN号['+obj.vin+']还未绑定发动机!')
          return false
        }
        const param = {}
        param.vin = obj.vin
        param.scanTrackNo = this.formModel.scanTrackNo
        this.post('/assemblePrintRecord/bindGearbox', param).then(_ => {
          this.formModel.scanTrackNo = ''
          this.$info('变速箱绑定成功!', this.queryTable)
        })
      },
      inputblur(){
        var _this = this;
        this.timeOut = setTimeout(function () {
          _this.$refs.scanTrackNo.focus()
        },2000);
      },
      initRules() {
        const REQUIRED_MSG = this.CONST.REQUIRED_MSG
        this.rules = {
          tmBasRouteId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        }
        this.$nextTick(_ => {
          this.$form.clearValidate()
        })
      },
      checkUserULoc(){
        const param = {}
        this.get('/assembleQueue/checkUserUloc', param).then(res => {
          this.formModel.uloc = res[0].ulocNo + '-' +res[0].ulocName
          this.formModel.ulocNo = res[0].ulocNo
          this.formModel.tmBasUlocId = res[0].tmBasUlocId
          this.disabledRoute = false
          this.inputblur()
          this.getRoute()
        })
      },
      getFreshenTime(){
        const param = {}
        param.paramKey = 'GP2'
        param.isDelete = '0'
        this.get('/globalParamsTbl/queryGlobalParamsTbl', param).then(res => {
          this.freshenTime = res.list[0].paramValue
        })
      },
      getRoute(){
        //通过当前工位编号查询所有的工艺路线
        const param1 = {pageNum:1,pageSize:9999}
        param1.isDelete = '0'
        param1.curUlocId = this.formModel.tmBasUlocId
        this.get('/tmBasRouteNode/queryTmBasRouteNode', param1).then(res => {
          this.routeOptions = res.list
        })
      },
      queryList() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.disabledRoute = true
            this.showSelect = false
            this.queryTable()
            this.timer = setInterval(this.queryTable, this.freshenTime);
          }
        })
      },
      queryTable(){
        this.inputblur()
        const param = {}
        param.ulocNo = this.formModel.ulocNo
        param.tmBasRouteId = this.formModel.tmBasRouteId
        this.$refs.table.query('/assemblePrintRecord/queryGearboxLaunchQueue', param, '', false)
      }
    }
  }
</script>

<style lang="scss" >

</style>

